using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using AandEDLL;
using AandEDLL.DataAccess;

namespace AandETester
{
	[TestFixture]
	public class TestHarness
	{
		[TestFixtureSetUp]
		public void TestFixtureSetUp()
		{
			
		}
		[TestFixtureTearDown]
		public void TestFixtureTearDown()
		{
			
		}
		[SetUp]
		public void SetUp()
		{
			
		}
		[TearDown]
		public void TearDown()
		{
			
		}

        [Test]
        public void AuthenticateUser()
        {
            User user = Accessor.AuthenticateUser("Admin", "Password");
            Assert.IsTrue(user != null);
        }

		[Test]
        public void Save_GetSaved_Delete_Items()
		{
            User user = Accessor.AuthenticateUser("Admin", "Password");
            List<Item> items = Accessor.GetSavedItems(user);
            int startingItemsCount = items.Count;

            System.Diagnostics.Debug.WriteLine("Items: " + items.Count);

            Item item = new Item();
            item.Currency = "US Dollar";
            item.Description = "Yo-yo";
            item.ItemName = "Fandangled Yo-Yo";
            item.Price = 12;
            item.SearchDate = DateTime.Now;
            item.SourceName = "Amazon";
            item.URL = "http://www.amazon.com/thisisnotarealURL";
            item.User = user;
            
            List<Item> list = new List<Item>();
            list.Add(item);

            Accessor.SaveItems(list);
            items = Accessor.GetSavedItems(item.User);

            System.Diagnostics.Debug.WriteLine("Items: " + items.Count);

            foreach (Item i in items)
            {
                if (i.ItemName == "Fandangled Yo-Yo")
                {
                    List<Item> toDeleteList = new List<Item>();
                    toDeleteList.Add(i);
                    Accessor.DeleteItems(toDeleteList);
                }
            }

            items = Accessor.GetSavedItems(item.User);
            System.Diagnostics.Debug.WriteLine("Items: " + items.Count);

            Assert.IsTrue(items.Count == startingItemsCount);
		}

        [Test]
        public void Save_Item_No_Description()
        {
            User user = Accessor.AuthenticateUser("Admin", "Password");
            List<Item> items = Accessor.GetSavedItems(user);
            int startingItemsCount = items.Count;

            System.Diagnostics.Debug.WriteLine("Items: " + items.Count);

            Item item = new Item();
            item.Currency = "US Dollar";
            item.ItemName = "Matt's Macbook Pro";
            item.Price = 2000;
            item.SearchDate = DateTime.Now;
            item.SourceName = "Yahoo";
            item.URL = "http://www.yahoo.com/apple/";
            item.User = user;

            List<Item> list = new List<Item>();
            list.Add(item);

            Accessor.SaveItems(list);
            items = Accessor.GetSavedItems(user);

            System.Diagnostics.Debug.WriteLine("Items: " + items.Count);

            foreach (Item i in items)
            {
                if (i.ItemName == "Matt's Macbook Pro")
                {
                    Console.Out.WriteLine("Item Name: " + i.ItemName);
                    Console.Out.WriteLine("Item Description: " + i.Description);
                    Console.Out.WriteLine("Item Price: " + i.Price);
                    List<Item> toDeleteList = new List<Item>();
                    toDeleteList.Add(i);
                    Accessor.DeleteItems(toDeleteList);
                }
            }

            items = Accessor.GetSavedItems(user);
            System.Diagnostics.Debug.WriteLine("Items: " + items.Count);

            Assert.IsTrue(items.Count == startingItemsCount);
        }

        [Test]
        public void Create_Select_Delete_User()
        {
            // Create object to insert new user
            User user = new User();
            user.UserName = "Josh";
            user.Password = "Moore";
            user.Admin = true;

            // Do insert
            user.UserID = Accessor.CreateUser(user);

            // Get all users
            List<User> users = Accessor.SelectUser();

            // Attempt to create a duplicate username/password set
            try
            {
                Accessor.CreateUser(user);
                Assert.Fail("User/pass combo already exists");
            }
            catch
            {
            }

            // delete the user
            Accessor.DeleteUser(new User[] { user });

            try
            {
                // select all users
                User temp = Accessor.GetUserByID(user.UserID);
                Assert.Fail("User still exists");
            }
            catch
            {
                Assert.IsTrue(true);
            }
        }
	}
}
